package cn.bytecollege.service;

import cn.bytecollege.mapper.CartMapper;
import cn.bytecollege.mapper.GoodsMapper;
import cn.bytecollege.mapper.OrderDetailMapper;
import cn.bytecollege.mapper.OrderMapper;
import cn.bytecollege.model.Cart;
import cn.bytecollege.model.OrderDetail;
import cn.bytecollege.model.Orders;
import cn.bytecollege.vo.CartVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
@Service
public class CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 添加商品到购物车
     * @param cart
     * @return
     */
    public Integer addCart(Cart cart) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", cart.getUserId());
        map.put("goods_id", cart.getGoodsId());
        List<Cart> carts = cartMapper.selectByMap(map);
        if (carts.size()==0) {
            return cartMapper.insert(cart);
        }
        Cart userCart = carts.get(0);
        if (userCart == null) {
            return cartMapper.insert(cart);
        }
        userCart.setGoodsNumber(userCart.getGoodsNumber()+ cart.getGoodsNumber());
        return cartMapper.updateById(userCart);
    }

    public Cart getCart(Integer cartId) {
        return cartMapper.selectById(cartId);
    }
    /**
     * 查看购物车
     * @param userId 用户Id
     * @return
     */
    public List<CartVO> getCarts(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);

        List<Cart> carts = cartMapper.selectByMap(map);
        List<CartVO> cartVOs = new ArrayList<>();

        for (Cart cart : carts) {
            CartVO cartVO = new CartVO();
            BeanUtils.copyProperties(cart,cartVO);
            cartVO.setGoods(goodsMapper.selectById(cart.getGoodsId()));
            cartVOs.add(cartVO);
        }
        return cartVOs;
    }

    /**
     * 更新购物车商品数量
     * @param cart cart中包含userId和goodsNumber
     * @return
     */
    public Integer modCart(Cart cart) {
        return cartMapper.updateById(cart);
    }

    /**
     * 删除购物车
     * @param cartId 购物车Id
     * @return
     */
    public Integer delCart(Integer cartId) {
        return cartMapper.deleteById(cartId);
    }

    /**
     *
     * @param orders orders:userId, order_state, order_date user_address_id
     * @param map map<key,value> key:goodsId value:goodsNumber
     * @return
     */
    @Transactional
    public Integer addOrders(Orders orders, HashMap<Integer,Integer> map){
        orderMapper.insert(orders);
        Integer orderId = orders.getOrderId();
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setGoodsId(entry.getKey());
            orderDetail.setGoodsNumber(entry.getValue());
            log.info(goodsMapper.selectById(entry.getKey()));
            log.info(goodsMapper.selectById(entry.getKey()).getGoodsPrice());
            orderDetail.setGoodsPrice(goodsMapper.selectById(entry.getKey()).getGoodsPrice());
            orderDetailMapper.insert(orderDetail);
        }
        return orders.getOrderId();
    }



    public List<Cart> queryWrapper(Cart cart) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>(cart, "cart_id");
        List<Cart> carts = cartMapper.selectList(queryWrapper);
        return carts;
    }
}
